package com.atguigu.guli.infrastructure.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;

@Configuration
public class CorssConfig {
    //配置跨域
    @Bean
    public CorsWebFilter corsWebFilter(){


        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //参数1： 需要进行跨域配置的路径   参数2：路径对应的跨域配置
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);//是否允许请求携带cookie
        config.addAllowedOrigin("*"); //允许哪些服务器跨域访问   *代表所有
        config.addAllowedMethod("*"); //允许的请求方式
        config.addAllowedHeader("*"); //允许携带任意的请求头方法
        corsConfigurationSource.registerCorsConfiguration("/**"  ,config);

        return new CorsWebFilter(corsConfigurationSource);
    }
}
